﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using fsw.Common;
using fsw.Web.UI;

namespace fsw.Web.Manager
{
    public partial class Add : Web.UI.ManagePage
    {
        protected Model.Admin model_admin;
        protected List<Model.Project> ListProject=new List<Model.Project>();
        protected List<Model.Mould> ListMould=new List<Model.Mould>();
        protected void Page_Load(object sender, EventArgs e)
        {
            model_admin = GetAdminInfo();//获取管理员信息
            if (model_admin.TypeId > 1) Response.End();
            ListProject = GetProjectList();
            ListMould = GetMouldList();
            if (DTRequest.IsPost())
            {
               Response.Write(DoAdd());
                Response.End();
            }
        }
        protected List<Model.Project> GetProjectList()
        {
            BLL.Project bll = new BLL.Project();
            return bll.GetList("", "OrderNo desc");
        }
        protected List<Model.Mould> GetMouldList()
        {
            BLL.Mould bll = new BLL.Mould();
            return bll.GetList("", "OrderNo asc");
        }
        private string DoAdd()
        {
            string UserName = DTRequest.GetFormString("UserName");//用户名
            string PassWord = DTRequest.GetFormString("PassWord");//密码
            string RealName = DTRequest.GetFormString("RealName");//中文名
            int TypeId = DTRequest.GetFormInt("TypeId");//管理员级别
            int isStatus = DTRequest.GetFormString("isStatus") == "on" ? 1 : 0;//锁定
            string[] UserProject = Request.Form.GetValues("UserProject");
            string[] UserModule = Request.Form.GetValues("UserModule");
            Model.Admin model = new Model.Admin();
            model.UserName = UserName;
            model.PassWord = Utils.MD5(PassWord);
            model.RealName = RealName;
            model.Logins = 0;
            model.AddTime = Utils.ConvertDateTimeInt(DateTime.Now);
            model.isStatus = isStatus;
            model.TypeId = TypeId;
            if (TypeId > 1)
            {
                List<Model.Admin_Project> _list = new List<Model.Admin_Project>();
                if (UserProject != null)
                {
                    for (int i = 0; i < UserProject.Length; i++)
                    {
                        int ProjectInt = Utils.StrToInt(UserProject[i], 0);
                        _list.Add(new Model.Admin_Project() { Pid = ProjectInt });
                    }                    
                }
                model.Admin_Project = _list;
                List<Model.Admin_Mould> _ListM = new List<Model.Admin_Mould>();
                if (UserModule != null)
                {
                    for (int j = 0; j < UserModule.Length; j++)
                    {
                        int _ModuleId = Utils.StrToInt(UserModule[j], 0);
                        _ListM.Add(new Model.Admin_Mould()
                        {
                            MouldId = _ModuleId
                        });
                    }
                }
                model.Admin_Mould = _ListM;
            }
            try {
                BLL.Admin bll = new BLL.Admin();
                if (bll.Exists(UserName))
                {
                    return JsonErr("error", "管理员" + UserName + "已存在!");
                }
                if (bll.Add(model) > 0)
                {
                    AdminAddLog(model_admin.UserName, "添加" + UserName + "管理员成功!");                   
                    return JsonErr("success", "添加管理员成功!");
                }
            }
            catch { }
            return JsonErr("error", "参数错误 ");
        }
    }
}